package com.tw.trips.ui;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;

import com.tw.trips.domain.Activity;
import com.tw.trips.domain.Comment;
import com.tw.trips.domain.Traveller;
import com.tw.trips.domain.Trip;
import com.tw.trips.service.TravellerService;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

@Service
@Configurable(preConstruction = true)
public class TripsPanel extends Panel implements ClickListener {

	@Autowired
	private TravellerService travellerService;

	Window subwindow;
	HorizontalSplitPanel subPanel;
	Traveller currentTraveller;

	public TripsPanel() {
		super();
		// TODO Auto-generated constructor stub
	}

	public TripsPanel(Traveller traveler, HorizontalSplitPanel subHorzSplitPanel) {
		setHeight("100%");
		setWidth("100%");
		currentTraveller = traveler;
		setCaption("My Trips!");

		subPanel = subHorzSplitPanel;

		VerticalLayout vert = new VerticalLayout();
		addComponent(vert);

		// for each trip
		for (Trip trip : traveler.getTrips()) {
			vert.addComponent(includeMainTripHorizLayout(trip));
			vert.addComponent(includeAddCommentPane(trip));
			vert.addComponent(LineBreak.getLinkeBreak());

			for (Activity activity : trip.getActivities()) {
				vert.addComponent(includeMainUpdateHorzLayout(activity));
				vert.addComponent(LineBreak.getLinkeBreak());
				for (Comment comment : activity.getComments()) {
					vert.addComponent(includeCommentHorzLayout(comment));
				}
			}

		}

	}

	private HorizontalLayout includeCommentHorzLayout(Comment comment) {
		HorizontalLayout mainUpdateHorzLayout = new HorizontalLayout();
		mainUpdateHorzLayout.setSpacing(true);

		// mainUpdateHorzLayout.setWidth("500px");
		/*Embedded commentedByPic = new Embedded("", new ThemeResource(
				"../runo/icons/16/user.png"));*/
		
		Embedded commentedByPic = new Embedded("", new ExternalResource(MainPage.IMG_URL+comment.getCommentedBy().getEmailID()+".jpg"));
		commentedByPic.setHeight("40px");
		commentedByPic.setWidth("40px");
		
		
		Label commentedBy = new Label(comment.getCommentedBy().getName());
		Label commentTxt = new Label(comment.getMessage());

		Embedded itHelps = new Embedded("", new ThemeResource(
				"../runo/icons/16/arrow-up.png"));
		Label itHelpsCount = new Label("" + comment.getItHelps().size());

		mainUpdateHorzLayout.addComponent(commentedByPic);
		mainUpdateHorzLayout.setComponentAlignment(commentedByPic,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(commentedBy);
		mainUpdateHorzLayout.setComponentAlignment(commentedBy,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(commentTxt);
		mainUpdateHorzLayout.setComponentAlignment(commentTxt,
				Alignment.MIDDLE_CENTER);
		// TODO : include it helps count
		mainUpdateHorzLayout.addComponent(itHelps);
		mainUpdateHorzLayout.setComponentAlignment(itHelps,
				Alignment.MIDDLE_RIGHT);

		mainUpdateHorzLayout.addComponent(itHelpsCount);
		mainUpdateHorzLayout.setComponentAlignment(itHelpsCount,
				Alignment.BOTTOM_CENTER);

		return mainUpdateHorzLayout;
	}

	private Component includeMainTripHorizLayout(Trip trip) {
		HorizontalLayout mainUpdateHorzLayout = new HorizontalLayout();
		mainUpdateHorzLayout.setSpacing(true);

		Label tripSource = new Label("From " + trip.getSource().getName());
		Label dept = new Label("Dept " + trip.getTravelStartDate());
		Label tripDest = new Label("To " + trip.getDestination().getName());
		Label arrival = new Label("Arrival " + trip.getTravelEndDate());

		mainUpdateHorzLayout.addComponent(tripSource);
		mainUpdateHorzLayout.setComponentAlignment(tripSource,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(dept);
		mainUpdateHorzLayout.setComponentAlignment(dept,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(tripDest);
		mainUpdateHorzLayout.setComponentAlignment(tripDest,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(arrival);
		mainUpdateHorzLayout.setComponentAlignment(arrival,
				Alignment.MIDDLE_CENTER);

		return mainUpdateHorzLayout;
	}

	private HorizontalLayout includeMainUpdateHorzLayout(Activity update) {
		HorizontalLayout mainUpdateHorzLayout = new HorizontalLayout();
		mainUpdateHorzLayout.setSpacing(true);
		Embedded updatedByPic = new Embedded("", new ExternalResource(MainPage.IMG_URL+update.getUpdatedBy().getEmailID()+".jpg"));
		updatedByPic.setHeight("40px");
		updatedByPic.setWidth("40px");
		Label updatedByLabel = new Label(update.getUpdatedBy().getName());
		Label updateTxt = new Label(update.getMessage());
		Embedded itHelps = new Embedded("", new ThemeResource(
				"../runo/icons/32/arrow-up.png"));

		Label itHelpsCount = new Label();
		if (update.getItHelps() != null) {
			itHelpsCount.setCaption("" + update.getItHelps().size());
		} else {
			itHelpsCount.setCaption("0");
		}

		mainUpdateHorzLayout.addComponent(updatedByPic);
		mainUpdateHorzLayout.setComponentAlignment(updatedByPic,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(updatedByLabel);
		mainUpdateHorzLayout.setComponentAlignment(updatedByLabel,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(updateTxt);
		mainUpdateHorzLayout.setComponentAlignment(updateTxt,
				Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(itHelps);
		mainUpdateHorzLayout.setComponentAlignment(itHelps,
				Alignment.MIDDLE_RIGHT);

		mainUpdateHorzLayout.addComponent(itHelpsCount);
		mainUpdateHorzLayout.setComponentAlignment(itHelpsCount,
				Alignment.BOTTOM_CENTER);
		return mainUpdateHorzLayout;
	}

	private Component includeAddCommentPane(Trip trip) {
		// Button w/ text and tooltip
		Button addActivityButton = new Button("Add Activity");
		addActivityButton.setStyleName(BaseTheme.BUTTON_LINK);
		addActivityButton.setDescription("To add an activity!");
		addActivityButton.addListener(this); // react to clicks
		addActivityButton.setData(trip);

		return addActivityButton;
	}

	public void buttonClick(ClickEvent event) {
		// Create the window...
		subwindow = new Window("Your say?");
		// ...and make it modal
		subwindow.setModal(true);
		subwindow.setHeight("50px");
		subwindow.setWidth("400px");
		subwindow.setData(((Button) event.getSource()).getData());
		// Configure the windws layout; by default a VerticalLayout
		VerticalLayout layout = (VerticalLayout) subwindow.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);

		TextArea editor = new TextArea(null, "");
		editor.setRows(2);
		editor.setColumns(28);
		layout.addComponent(editor);

		Button addActivityButton = new Button("Add Activity",
				new Button.ClickListener() {
					// inline click-listener
					public void buttonClick(ClickEvent event) {
						Trip trip = (Trip) subwindow.getData();
						VerticalLayout layout = (VerticalLayout) subwindow
								.getContent();
						TextArea txtArea = (TextArea) layout.getComponent(0);
						String updateTxt = (String) txtArea.getValue();

						// Update update = new Update();
						// update.setMessage(updateTxt);
						// update.setCreatedOn(new Date());
						// update.setUpdatedBy(currentTraveller);
						// trip.getActivities().add(update);
						Activity activity = new Activity(
								updateTxt, "Img URL",
								new Date(), currentTraveller);
						currentTraveller = travellerService.addActivityToTrip(
								trip, activity);

						subPanel.setFirstComponent(new TripsPanel(
								currentTraveller, subPanel));
						(subwindow.getParent()).removeWindow(subwindow);
					}
				});
		// The components added to the window are actually added to the window's
		// layout; you can use either. Alignments are set using the layout
		layout.addComponent(addActivityButton);
		layout.setComponentAlignment(addActivityButton, Alignment.TOP_RIGHT);
		getWindow().addWindow(subwindow);

	}
}
